function plusReady() {
	window.addEventListener('show', function(event) {
		init();
	});
	window.addEventListener('preshow', function(event) {
		console.log('cartpreshow');
		init();
	});
	$('.peisong-choose').click(function() {
		$('.peisong-choose').removeClass('active');
		$(this).addClass('active');
	});
}

function init() {
	plus.navigator.setStatusBarBackground("#27b6f0");
	is_login();
	mui.get(base_url + 'api/user/getaddress', {

	}, function(data) {
		console.log(data.code);
		if(data.code == 0) {
			$('.lianxiren').html(data.data.name);
			$('.mobile').html(data.data.mobile);
			$('.dizhi').html(data.data.xiaoqu + data.data.address);
		}
	}, 'json');
	if(plus.storage.getItem('mobile') != 0) {

	} else {
		console.log('gologin1');
		openNewWindows('login.html');
	}
	getmycats();

	function getmycats() {
		mui.get(base_url + 'api/user/getcats', {

		}, function(data) {
			var html = "";
			var zongjianshu = 0;
			var zonge = 0;
			$.each(data.data, function(index, value) {
				if(value.store_count <= value.num) {
					isdisabled = 'disabled'
				} else {
					isdisabled = '';
				}
				zongjianshu += value.num;
				zonge += value.zonge;
				html += '<div class="goods-item">' +
					'	<div class="fl goods-thumb">' +
					'		<img style="width:87px;height:87px;" src="' + base_url + value.main_img + '?x-oss-process=image/resize,m_fixed,h_87,w_87">' +
					'	</div>' +
					'	<div class="fl goods-info">' +
					'		<li class="title">' +
					'			<a href="##">' + value.goods_name + '</a>' +
					'		</li>' +
					'		<li class="price"><span>￥' + value.shop_price + '</span>元/' + value.sku + '</li>' +
					'		<li class="stock">库存' + value.store_count + '瓶 已售' + value.sales_sum + value.sku + '</li>' +
					'	</div>' +
					'	<div class="fr goods-number" data-stock="' + value.store_count + '" data-price="' + value.shop_price + '">' +
					'		<a href="javascript:;" class="weui-icon-less "></a>' +
					'		<span data-id="' + value.goods_id + '" class="num">' + value.num + '</span>' +
					'		<a href="javascript:;" class="weui-icon-add ' + isdisabled + '"></a>' +
					'	</div>' +
					'	<div class="clearfix"></div>' +
					'</div>';
			});
			html += '<div id="total-info">' +
				'商品合计：共<span id="total-number">' + zongjianshu + '</span>件 - ￥<span id="total-price">' + zonge + '</span>元' +
				'</div>';
			if(zonge > 10) {
				var peisongfei = 0;
			} else {
				var peisongfei = 6;
			}
			$('#goods-items').html(html);
			$('#peisong-price').html(peisongfei);

			if(zongjianshu > 0 && zonge > 0) {
				
			} else {
				$("#btn-submit").addClass("disabled");
			}

			$('#pay-price').html(zonge);
			$('#pay-peisong').html('（含配送费' + peisongfei + '元）');
			var goods = {};
			goods.num = zongjianshu;
			goods.price = zonge;

			//商品数量
			$(".goods-number a").on('click', function() {
				//			console.log('123');
				if($(this).hasClass("disabled")) {
					return false;
				}
				var box = $(this).parents(".goods-number");
				var stock = parseInt(box.attr("data-stock"));
				var num = parseInt(box.find(".num").text());
				var price = parseFloat(box.attr("data-price"));

				if($(this).hasClass("weui-icon-less")) {
					if(num > 0) {
						goods.num = goods.num - 1;
						goods.price = goods.price - price;
						box.find(".num").text(num - 1);
					}
				}
				if($(this).hasClass("weui-icon-add")) {
					if(num < stock) {
						goods.num = goods.num + 1;
						goods.price = goods.price + price;
						box.find(".num").text(num + 1);
					}
				}
				goods.price = Math.round(goods.price * 10) / 10;
				num = parseInt(box.find(".num").text());
				if(num < 1) {
					box.find(".weui-icon-less").addClass("disabled");
				} else {
					box.find(".weui-icon-less").removeClass("disabled");
				}
				if(num == stock) {
					box.find(".weui-icon-add").addClass("disabled");
				} else {
					box.find(".weui-icon-add").removeClass("disabled");
				}
				$("#total-number").text(goods.num);
				$("#total-price").text(goods.price);
				if(goods.num > 0 && goods.price > 0) {
					$("#btn-submit").removeClass("disabled");
				} else {
					$("#btn-submit").addClass("disabled");
				}
				if($("#peisong-info").length > 0) {
					var peisong_price = parseFloat($("#peisong-price").text());
					var peisong_free = parseFloat($("#peisong-free").text());
					if(goods.price > peisong_free) {
						price = goods.price;
					} else {
						console.log(peisong_free);
						price = goods.price + peisong_price;
					}
					$("#pay-price").text(price);
					if(goods.price > 0) {
						$("#topay-btn a").removeClass("disabled");
					} else {
						$("#topay-btn a").addClass("disabled");
					}
					if(price > 10) {
						var peisongfei = 0;
					} else {
						var peisongfei = 6;
					}
					$('#peisong-price').html(peisongfei);
					$('#pay-price').html(price);
					$('#pay-peisong').html('（含配送费' + peisongfei + '元）');
				}

			})
		}, 'json');
	}
}
document.addEventListener("plusready", plusReady, false);